package com.cinemtl.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.cinemtl.beans.User;
import com.cinemtl.manager.UserManager;
import com.google.gson.Gson;

public class UserAction {

	public static void addUser(User user) {

		UserManager.insert(user);

	}

	public static void showUser(String username, HttpServletRequest request,
			HttpServletResponse response) {
		User user = UserManager.getByUsername(username);
		request.setAttribute("user", user);

		try {
			request.getRequestDispatcher("user.jsp").forward(request, response);
		} catch (ServletException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static boolean verifyUser(HttpServletRequest request,
			HttpServletResponse response, String username, String password) {
		boolean verified = false;

		if (UserManager.verifyUser(username, password)) {
			HttpSession session = request.getSession();
			session.setAttribute("user", UserManager.getByUsername(username));
			verified = true;
		}
		return verified;

	}

	public static boolean checkUsername(String username) {
		return UserManager.checkAvailable(username);
	}

	public static String getUserProfile(String username) {

		Gson gson = new Gson();
		User user = UserManager.getByUsername(username);
		String reponse = gson.toJson(user);

		return reponse;
	}

	public static boolean checkEmail(String email) {
		return UserManager.verifyEmail(email);

	}
}
